//
//  MessagesFrame.m
//  QQ聊天
//
//  Created by 伍银荣 on 2021/6/8.
//  Copyright © 2021 com.example. All rights reserved.
//

#import "MessagesFrame.h"
#import <UIKit/UIKit.h>
#import "Messages.h"

#define Width [UIScreen mainScreen].bounds.size.width
#define timeFont [UIFont systemFontOfSize:19]
#define textFont [UIFont systemFontOfSize:18]

@implementation MessagesFrame

-(void)setMessages:(Messages *)messages {
    _messages = messages;
    //设置frame
    [self setupFrame];
}

- (void)setupFrame {
    CGFloat margin = 10;
    //时间
    CGFloat timeW = Width;
    CGFloat timeH = 25;
    CGFloat timeX = 0;
    CGFloat timeY = margin;
    if (!_messages.hideTime) {
        _timeFrame = CGRectMake(timeX, timeY, timeW, timeH);
    }
    
    
    //icon
    CGFloat iconW = 50;
    CGFloat iconH = iconW;
    CGFloat iconY = CGRectGetMaxY(_timeFrame) + margin;
    CGFloat iconX;
    if (_messages.type == 0) {
        iconX = Width - margin - iconW;
    } else {
        iconX = margin;
    }
    _iconFrame = CGRectMake(iconX, iconY, iconW, iconH);
    
    //正文btn
    CGSize textSize = [_messages.text boundingRectWithSize:CGSizeMake(300, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName: textFont} context:nil].size;
    CGFloat textW = textSize.width;
    CGFloat textH = textSize.height;
    CGFloat textY = iconY + margin;
    CGFloat textX;
    if (_messages.type == 0) {
        textX = Width - 60 - margin - textW;
    } else {
        textX = CGRectGetMaxX(_iconFrame) + margin;
    }
    _textFrame = CGRectMake(textX, textY, textW, textH);
    //    _textBtn.center = CGPointMake(textX, iconH * 0.5);
    NSLog(@"x = %f, w = %f , h = %f",textX, textW,textH);
    
    CGFloat rowY = MAX(CGRectGetMaxY(_iconFrame), CGRectGetMaxY(_textFrame));
    _rowHeight = rowY + margin;
}

@end
